﻿using System;
using System.Text;
using System.Data.SqlClient;
//using System.Data.SQLite;
using System.Data.SQLite;
using System.Net.Sockets;
using System.Net;

namespace DATA_SERVER
{
    abstract class Service
    {
        public bool isStop = true;
        public abstract void AddUser(string pin, string name, string cardno, string type, string extra);

        public abstract void DelUser(string pin);

        public abstract void ClearUser();

        public abstract void StartService();

        public abstract void StopService();

        public abstract string CheckConnect();

        //门禁记录处理函数
        public static void GetRecord(string pin, string time, int DeviceNo, string strSN, string BeaconName)
        {
            string staffno = string.Empty;
            string dspstr = string.Empty;
            string msgstr = string.Empty;
           
            try
            {
                //从本地数据库读取人员信息
                SQLiteDataReader reader = DB_SQLite.ExecuteReader(string.Format("SELECT [StaffNo],[TagMac],[StaffName],[DeptName],[TypeJobName],[PositionsName],[StaffPhotoPath] FROM [T_SM_Staff] WHERE [Pin] = '{0}'", pin));
                //DB_SQLite.ExecuteReader(string.Format("DELETE FROM [T_SM_Staff] WHERE StaffNO = '12221' "));
                if (reader.Read())
                {
                    staffno = reader[0].ToString();
                    dspstr += "RECORD\r\n" + time + "\t" + DeviceNo + "\t" + string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t", reader[0], reader[1], reader[2], reader[3], reader[4], reader[5], reader[6]);
                    msgstr += "工号:" + reader[0] + " 卡号:" + reader[1] + " 姓名:" + reader[2] + " 时间:" + time;
                    reader.Close();
                }
                else
                {
                    msgstr += " 未知用户通过";
                    reader.Close();
                    Data_Func.ShowText(msgstr);
                    return;
                }

                //发送到显示端
                Byte[] bSendData = Encoding.GetEncoding("utf-8").GetBytes(dspstr);
                Display_Func.SendToDisplay(bSendData);

                //如果有人员定位系统，则发送消息给人员定位系统
                if (Global.HaveCenter)
                {
                    string centermsg = "@|" + staffno + "|" + time + "|" + strSN + "|$";
                    Net_Func.SendUDP(Encoding.GetEncoding("utf-8").GetBytes(centermsg), Global.CenterEP);
                }

                msgstr += " 在 <" + BeaconName + "> 处通过";
                Data_Func.ShowText(msgstr);
            }
            catch
            {
                msgstr += "记录解析失败";
                Data_Func.ShowText(msgstr);
            }

            //将门禁记录加入保存队列
            Data_Thread.SaveRecord(staffno, time, strSN);
        }

        //获取所有门禁机的状态并向显示端发送
        public static void GetStatus(object obj)
        {
            Service myService = obj as Service;
            string statstr = "STATUS\r\n" + myService.CheckConnect();
            Byte[] bSendData = Encoding.GetEncoding("utf-8").GetBytes(statstr);

            //存储门禁机状态记录到日志内
            Data_Func.SaveLog(statstr);

            Display_Func.SendToDisplay(bSendData);
        }

        //根据人员编号和门禁机类型获取人员额外信息序列化字符串
        public static string GetUser(string pin, string type)
        {
            string extra = string.Empty;
            try
            {
                SQLiteDataReader reader = DB_SQLite.ExecuteReader(string.Format("SELECT [Extra] FROM [T_SM_StaffExtra] WHERE [Pin] = '{0}' AND [Type] = '{1}'", pin, type));
                if (reader.Read())
                {
                    extra = reader[0].ToString();
                }
                reader.Close();
            }
            catch { }
            if (extra.Length <= 0)
            {
                try
                {
                    SqlDataReader reader = DB_SQLServer.ExecuteReader(string.Format("SELECT [Extra] FROM [T_SM_StaffExtra] WHERE [Pin] = '{0}' AND [Type] = '{1}'", pin, type));
                    if (reader.Read())
                    {
                        extra = reader[0].ToString();
                    }
                    reader.Close();
                }
                catch { }
            }
            return extra;
        }

        //更新新用户到数据库
        public static void SaveUser(string pin, string type, string extra)
        {
            try
            {
                DB_SQLite.ExecuteNonQuery(string.Format("REPLACE INTO [T_SM_StaffExtra] ([Pin],[Type],[Extra]) VALUES('{0}','{1}','{2}')", pin, type, extra));
            }
            catch { }
        }
    }
}
